package com.itheima.controller;

import com.itheima.config.R;
import com.itheima.utils.AliOSSUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;


@RestController
@RequestMapping("/common")
public class CommonController {

    @Value("${reggie.path}")
    private String path;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file){

        // 获取 前端图片的名称 149641.jpg
        String filename = file.getOriginalFilename();

        // 获取 .jpg字符串
        String substring = filename.substring(filename.lastIndexOf("."));

        //获取 UUid  并且和 .jpg字符串 拼接
        String uuid = UUID.randomUUID().toString()+substring;

        // 判断D盘 imp文件夹 是否存在 不存在就创建  存在就不创建 直接上传图片
//        File dir = new File(path);
//        if(!dir.exists()){
//            dir.mkdirs();
//        }


        try {
            // 上传操作
 //           file.transferTo(new File(path+uuid));
            aliOSSUtils.uploadFile(uuid,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 返回文件名 方便  下载时候根据名字查询
        return R.success(uuid);
    }

    /**
     * 下载图片
     * @param name
     * @param response
     * @throws IOException
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) throws IOException {


        aliOSSUtils.downLoadFile(name,response.getOutputStream());

//        InputStream inputStream = null;
//        ServletOutputStream outputStream = null;
//        try {
//
//            inputStream = new FileInputStream(new File(path+name));
//            outputStream = response.getOutputStream();
//
//            byte[] bw = new byte[1024];
//            int len = 0;
//            while ((len = inputStream.read(bw)) != -1 ){
//                    outputStream.write(bw,0,len);
//                    outputStream.flush();
//            }
//
//        }finally {
//            try {
//                if (inputStream != null)
//                    inputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//            try {
//                if (outputStream != null)
//                    outputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

        // 第二种写法
//      IOUtils.copy(new FileInputStream(new File(path+name)),response.getOutputStream());

    }
}
